package com.hqd.ch03.v49.context.annotation;

import com.hqd.ch03.utils.ClassUtils;
import com.hqd.ch03.v49.beans.factory.config.BeanDefinition;
import com.hqd.ch03.v49.factory.support.BeanNameGenerator;
import com.hqd.ch03.v49.registry.BeanDefinitionRegistry;

import java.beans.Introspector;

public class AnnotationBeanNameGenerator implements BeanNameGenerator {
    public static final BeanNameGenerator INSTANCE = new AnnotationBeanNameGenerator();

    @Override
    public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
        return "";
    }

    protected String buildDefaultBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
        return buildDefaultBeanName(definition);
    }

    /**
     * Derive a default bean name from the given bean definition.
     * <p>The default implementation simply builds a decapitalized version
     * of the short class name: e.g. "mypackage.MyJdbcDao" &rarr; "myJdbcDao".
     * <p>Note that inner classes will thus have names of the form
     * "outerClassName.InnerClassName", which because of the period in the
     * name may be an issue if you are autowiring by name.
     *
     * @param definition the bean definition to build a bean name for
     * @return the default bean name (never {@code null})
     */
    protected String buildDefaultBeanName(BeanDefinition definition) {
        String beanClassName = definition.getBeanClassName();
        String shortClassName = ClassUtils.getShortName(beanClassName);
        return Introspector.decapitalize(shortClassName);
    }
}
